<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Repozytorium dokumentów</title>
		<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css" />
		<link rel="stylesheet" type="text/css" href="/static/css/bootstrap-responsive.min.css" />
		<link rel="stylesheet" type="text/css" href="/static/css/custom.css" />
		<script type="text/javascript" src="/static/js/jquery-1.8.3.min.js"></script>
		<script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
	</head>
	<body>
		<div id="box">
			<div class="navbar">
				<div class="navbar-inner">
					<div class="container">
 						<a class="brand" href="/"><strong>Repozytorium</strong></a>
						<form action="documents" class="navbar-form  pull-left">
							<input class="input-xlarge" name="q" type="text" placeholder="Treść..." value="${q}">
							<button class="btn btn-medium btn-primary" type="submit">Szukaj</button>
						</form>
						<a class="btn btn-medium pull-right" data-toggle="collapse" data-target="#file">
							Dodaj
						</a>
					</div>
				</div>
			</div>
			
			<div id="file" class="collapse">
				<form action="/document/add" method="post" enctype="multipart/form-data">
					<input id="true_file" type="file" name="file" multiple>
					<button class="btn" type="submit"><i class="icon-upload"></i></button> 
				</form>
			</div>
			
			<%@ include file="Messages.jsp" %>
			<%@ include file="Filters.jsp" %>
			
			<div id="documents">
			<c:if test="${page.hasContent()}">
				<table class="table table-condensed table-bordered">
					<thead>
						<tr>
							<td>Nazwa pliku</td>
							<td>Format</td>
							<td>Data dodania</td>
							<td>Edytuj</td>
							<td>Usuń</td>
							<td>Pobierz</td>
						</tr>
					</thead>
					<tbody>
						<c:forEach var="document" items="${page.content}">
							<tr>
								<td>${document.filename}</td>
								<td>${document.format}</td>
								<td>${document.added}</td>
								<td>
									<a class="btn" href="<c:url value="/document/${document.id}/show"></c:url>">
										<i class="icon-edit"></i>
									</a>
								</td>
								<td>
									<a class="btn" href="<c:url value="/document/${document.id}/delete"></c:url>">
										<i class="icon-remove"></i>
									</a>
								</td>
								<td>
									<a class="btn" href="<c:url value="/document/${document.id}/download"></c:url>">
										<i class="icon-download"></i>
									</a>
								</td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
				
				<div class="pagination pagination-centered">
					<ul>
						<s:url var="firstUrl" value="/documents">
							<s:param name="p">1</s:param>
							<s:param name="q">${q}</s:param>
							<c:forEach var="format" items="${formats}">
							<s:param name="format">${format}</s:param>
							</c:forEach>
							
							
						</s:url>
						<s:url var="prevUrl" value="/documents">
							<s:param name="p">${page.number}</s:param>
							<s:param name="q">${q}</s:param>
							<c:forEach var="format" items="${formats}">
							<s:param name="format">${format}</s:param>
							</c:forEach>
						</s:url>
						<s:url var="nextUrl" value="/documents">
							<s:param name="p">${page.number + 2}</s:param>
							<s:param name="q">${q}</s:param>
							<c:forEach var="format" items="${formats}">
							<s:param name="format">${format}</s:param>
							</c:forEach>
						</s:url>
						<s:url var="lastUrl" value="/documents">
							<s:param name="p">${page.totalPages}</s:param>
							<s:param name="q">${q}</s:param>
							<c:forEach var="format" items="${formats}">
							<s:param name="format">${format}</s:param>
							</c:forEach>
						</s:url>
						<c:choose>
							<c:when test="${page.isFirstPage()}">
								<li class="disabled">
									<a href="${firstUrl}">«</a>
								</li>
								<li class="disabled">
									<a href="${firstUrl}">‹</a>
								</li>
							</c:when>
							<c:otherwise>
								<li>
									<a href="${firstUrl}">«</a>
								</li>
								<li>
									<a href="${prevUrl}">‹</a>
								</li>
							</c:otherwise>
						</c:choose>
						<c:forEach var="i" begin="${minPage}" end="${maxPage}">
							<s:url var="url" value="/documents" >
								<s:param name="p">${i + 1}</s:param>
								<s:param name="q">${q}</s:param>
								<c:forEach var="format" items="${formats}">
								<s:param name="format">${format}</s:param>
								</c:forEach>
							</s:url>
							<c:choose>
								<c:when test="${page.number == i}">
									<li class="active">
										<a href="${url}">${i + 1}</a>
									</li>
								</c:when>
								<c:otherwise>
									<li>
										<a href="${url}">${i + 1}</a>
									</li>
								</c:otherwise>
							</c:choose>
						</c:forEach>
						<c:choose>
							<c:when test="${page.isLastPage()}">
								<li class="disabled">
									<a href="${lastUrl}">›</a>
								</li>
								<li class="disabled">
									<a href="${lastUrl}">»</a>
								</li>
							</c:when>
							<c:otherwise>
								<li>
									<a href="${nextUrl}">›</a>
								</li>
								<li>
									<a href="${lastUrl}">»</a>
								</li>
							</c:otherwise>
						</c:choose>
					</ul>
				</div>
				</c:if>
			</div>
		</div>
	</body>
</html>